import gcRequest from '..'
import { articleDetailRes, articleListRes, createArticleRes } from './types'

export const getArticleList = (offect: number, limit: number) => {
  return gcRequest.get<articleListRes.RootObject>({
    url: '/article',
    params: {
      offect,
      limit
    }
  })
}

export const getArticleDetail = (id: number) => {
  return gcRequest.get<articleDetailRes.RootObject>({
    url: `/article/${id}`
  })
}

export const createArticle = (
  name: string,
  content: string,
  imgList: string[],
  tagId: number
) => {
  return gcRequest.post<createArticleRes.RootObject>({
    url: '/article',
    data: {
      name,
      content,
      imgList,
      tagId
    }
  })
}

export const getArticlesByTagId = (
  tagId: number,
  { offect, limit }: { offect: number; limit: number }
) => {
  return gcRequest.get<articleListRes.RootObject>({
    url: `/article/tag/${tagId}`,
    params: {
      offect,
      limit
    }
  })
}

export const searchArticle = (
  word: string,
  { offect, limit }: { offect: number; limit: number }
) => {
  return gcRequest.get<articleListRes.RootObject>({
    url: '/article/search',
    params: {
      word,
      offect,
      limit
    }
  })
}

export const removeArticleById = (id: number) => {
  return gcRequest.delete({
    url: `/article/${id}`
  })
}

export const findArticlesByUserId = (
  id: number,
  { offect, limit }: { offect: number; limit: number }
) => {
  return gcRequest.get<articleListRes.RootObject>({
    url: `/article/user/${id}`,
    params: {
      offect,
      limit
    }
  })
}
